<?php
/**
 * Created by PhpStorm.
 * User: MI
 * Date: 2018/10/23
 * Time: 10:55
 */

namespace App\Http\Controllers\Course;


use App\Http\Controllers\Controller;
use App\Models\Course;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CourseController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $user = Auth::user();
        $category = $request->get('category');
        $status = $request->get('status');
        $type = $request->get('type');
        $model = Course::query()->whereHas('schoolManager', function ($query) use ($user) {
                $query->where('schools.id', $user->student->school_id);
            })
            ->when($category, function ($query) use ($category) {
                $query->where('category_id', $category);
            })
            ->when($type, function ($query) use ($type) {
                $query->whereHas('lessons', function ($q) use ($type) {
                    $q->where('status', $type);
                });
            })
            ->paginate(18);
        return view('course.index', compact('model', 'category', 'status', 'type'));
    }

    /**
     * @param Course $course
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function detail(Course $course)
    {
        return view('course.detail', compact('course'));
    }

}